package com.xudow.app.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.activeshare.edu.ucenter.models.qanda.Answer;
import com.activeshare.edu.ucenter.models.qanda.Question;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.ui.task.qanda.QuestionDetailTask;
import com.xudow.app.util.DateUtil;
import com.xudow.app.util.ImageUtils;
import com.xudow.app.util.Lists;
import com.xudow.app.util.Maps;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 问题详情Activity
 *
 * @author lukecheng
 */
public class QuestionDetailActivity extends BaseActivity {

    private long questionId;
    private Question question;
    private Answer satisfiedAnswer;
    private List<Answer> answerList;

    private QuestionDetailTask questionDetailTask;

    private CircleImageView portraitView;
    private TextView usernameView, answerCountView, timeView, questionView;
    private LinearLayout satisfiedAnswerLayout;
    private TextView satisfiedUsername, satisfiedTime, satisfiedContent;
    private LinearLayout answersLayout, answersView;

    private LayoutInflater layoutInflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question_detail);

        questionId = getIntent().getLongExtra("questionId", 0L);

        portraitView = (CircleImageView) findViewById(R.id.portrait);
        usernameView = (TextView) findViewById(R.id.username);
        answerCountView = (TextView) findViewById(R.id.answer_count);
        timeView = (TextView) findViewById(R.id.time);
        questionView = (TextView) findViewById(R.id.question);

        satisfiedAnswerLayout = (LinearLayout) findViewById(R.id.satisfied_answer_layout);
        satisfiedUsername = (TextView) findViewById(R.id.satisfied_answer_username);
        satisfiedTime = (TextView) findViewById(R.id.satisfied_answer_time);
        satisfiedContent = (TextView) findViewById(R.id.satisfied_answer_content);

        answersLayout = (LinearLayout) findViewById(R.id.answers_layout);
        answersView = (LinearLayout) findViewById(R.id.answers);

        layoutInflater = getLayoutInflater();

        HashMap<String, String> param = Maps.newHashMap();
        param.put("question_id", Long.toString(questionId));

        showLodingDialog(getString(R.string.loading));

        questionDetailTask = new QuestionDetailTask(this, questionDetailTaskHandler);
        addTask(questionDetailTask);
        questionDetailTask.execute(param);
    }

    public void onBackBtnClick(View view) {
        finish();
    }

    private Handler questionDetailTaskHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            hideLoadingDialog();

            if (QuestionDetailTask.SUCCESS == msg.what) {
                Question[] dataArr = (Question[]) msg.getData().getSerializable("questions");

                if (null == dataArr || dataArr.length <= 0) {
                    return;
                }

                question = dataArr[0];

                if (question.getAnswers() != null && question.getAnswers().size() > 0) {
                    answerList = Lists.newArrayList();

                    for (Answer answer : question.getAnswers()) {
                        if (1 == answer.getIsSatisfied()) {
                            satisfiedAnswer = answer;
                        } else {
                            answerList.add(answer);
                        }
                    }
                }

                initViews();
            }
        }
    };

    private void initViews() {
        if (null == question) {
            return;
        }

        String portraitUrl = String.format(Config.IMAGE_LOAD_URL_PARAMS, question.getUserProfile().getPhotoPath());
        ImageUtils.loadImage(this, portraitView, portraitUrl, R.mipmap.def_portrait);

        //String time = MessageUtils.formatTimeStampString(this, question.getCreateTime());
        String time = DateUtil.dateTimeToStr(question.getCreateTime(), DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        timeView.setText(time);

        usernameView.setText(question.getUserProfile().getName());
        questionView.setText(question.getQuestion());

        //有几个人回答
        int answerCount = 0;

        if (question.getAnswers() != null && question.getAnswers().size() > 0) {
            Map<Long, String> answerUserMap = Maps.newHashMap();
            for (Answer a : question.getAnswers()) {
                if (answerUserMap.get(a.getUserProfileId()) == null) {
                    answerUserMap.put(a.getUserProfileId(), "");
                    answerCount++;
                }
            }
        }

        Spanned answerCountSpanned = Html.fromHtml(
                String.format("已有<font color=\"#7fc524\">%d</font>人回答", answerCount));
        answerCountView.setText(answerCountSpanned);

        if (null == satisfiedAnswer) {
            satisfiedAnswerLayout.setVisibility(View.GONE);
        } else {
            satisfiedAnswerLayout.setVisibility(View.VISIBLE);
            satisfiedUsername.setText(satisfiedAnswer.getUserProfile().getName());
            //satisfiedTime.setText(MessageUtils.formatTimeStampString(this, satisfiedAnswer.getCreateTime()));
            satisfiedTime.setText(DateUtil.dateTimeToStr(satisfiedAnswer.getCreateTime(), DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS));
            satisfiedContent.setText(satisfiedAnswer.getAnswer());
        }

        if (null == answerList || answerList.size() <= 0) {
            answersLayout.setVisibility(View.GONE);
        } else {
            answersLayout.setVisibility(View.VISIBLE);

            for (Answer answer : answerList) {
                answersView.addView(createAnswerView(answer));
            }
        }
    }

    private View createAnswerView(Answer answer) {
        View view = layoutInflater.inflate(R.layout.item_extra_answers_list, null);
        ((TextView) view.findViewById(R.id.username)).setText(answer.getUserProfile().getName());
        ((TextView) view.findViewById(R.id.time)).setText(
                DateUtil.dateTimeToStr(answer.getCreateTime(), DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS)
        );
        ((TextView) view.findViewById(R.id.content)).setText(answer.getAnswer());

        return view;
    }

}
